import { confirmOrder ,generateOrder } from '@/api/order.js';

export default {
	data() {
		return {
			totalAmount: 0,
			productItemList: [],
			skuCodes: [],
		}
	},
	onLoad(option) {
		//商品数据
		this.skuCodes = JSON.parse(option.skuCodes);
		this.loadData();
	},
	methods: {
		//生成确认单信息
		async loadData() {
			let data = {
				skuCodes : this.skuCodes
			}
			confirmOrder(data).then(response => {
				 this.productItemList = response.data;
				 //计算总金额
				 this.calcTotalAmount();
			});
		},
		
		//购买数量
		numberChange(numberData) {
			let cartItem = this.productItemList[numberData.index];
			cartItem.number = numberData.number;
			//计算总金额
			this.calcTotalAmount();
		},
		numberBlur(e) {
			console.log('blur:', e);
		},
		numberFocus(e) {
			console.log('focus:', e);
		},
		
		stopPrevent() {},
		
		//计算支付金额
		calcTotalAmount() {
			let total = 0;
			this.productItemList.forEach(item => {
				total += item.price * item.number;
			})
			this.totalAmount = Number(total.toFixed(2));
		},
		
		//创建订单
		submit() {
			let data = {
				productItemList:this.productItemList
			}
			generateOrder(data).then(response => {
				let orderSn = response.data;
				uni.showModal({
					title: '提示',
					content: '订单创建成功，是否要立即支付？',
					confirmText:'去支付',
					cancelText:'取消',
					success: function(res) {
						if (res.confirm) {
							uni.redirectTo({
								url: `/pages/order/pay/pay?orderSn=${orderSn}`
							})
						} else if (res.cancel) {
							uni.redirectTo({
								url: '/pages/order/list/list?state=1'
							})
						}
					}
				});
			});
		},
	}
}